<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Contracts\TestContract;
use Mail;
use Illuminate\Broadcasting\BroadcastManager;
use Illuminate\Support\Facades\Auth;
use App\User;
class IndexController extends Controller
{
    public $test;
    public function __construct(TestContract $test){
        $this->test=$test;
    }
    //提交页
    public function commitPage(){
        return view();
    } 
    //获取信息列表
    public function getExpressList(Request $request){
//         echo '获取信息列表';
//         die;
        $content=$request->input('content');

        $message=[
            'content.required'=>'内容不能为空'
        ];
        $rules=[
            'content'=>'required'
        ];
        $validator=Validator::make($_POST,$rules,$message);
        $info=array();
        $info=$validator->errors()->all();
        if(!empty($info)){
            renderInfo(400001,$info[0]);
        }
        $orderArray=explode(',',$content);

        $url="https://www.kuaidi100.com/query";
        $data=array(
            'type'=>'jd',
            'postid'=>$content,
            'temp'=>''
        );
        $infoList=array();
        foreach ($orderArray as $key=>$value){
            $data['postid']=$value;
            $info=httpRequest($url,$data);
            $info=json_decode($info,true);
//            var_dump($info);
//            die;
            $infoList[]=$info['data'][0];
        }


        renderData(0,'ok',$infoList);
       // dd($info);
    }
    //测试契约（contract）接口调用服务（service）
    public function test(){
        $this->test->callMe('IndexController');
    }

    //发送laravel邮件
    public function send()  
    {  
        $name = '学院君';  
        $flag = Mail::send('123456',['name'=>$name],function($message){  
            $to = '1072155122@qq.com';  
            $message ->to($to)->subject('测试邮件');  
        });  
        if($flag){  
            echo '发送邮件成功，请查收！';  
        }else{  
            echo '发送邮件失败，请重试！';  
        }  
    } 
    //laravel的广播测试
    public function broadcastTest(){
        $manager = app(BroadcastManager::class);
        $driver = $manager->connection();
        // 第一个参数是频道名，第二个参数是事件名，第三个参数是广播内容
        $driver->broadcast(['channel_1', 'channel_2'], 'login', ['message' => 'hello world']);
    }
    //用户auth函数进行测试
    public function users(){
        //dd(Auth::check());
        //用户的Id
        //dd(Auth::id());
       // dd(Auth::user()->toArray());
        //退出
        //Auth::logout(Auth::user());
        //登录生效一次
//        $rs=Auth::attempt(['email'=>'1105730947@qq.com','password'=>'123456']);
//        var_dump($rs);
        //dd(Auth::check());
       // $users=User::find(1);
       // dd($users);
        $rs=Auth::login(User::find(1));
        dd(Auth::user());
      //  dd($rs);

    }
}
